//
//  FromQQFriendViewController.m
//  Qya
//
//  Created by maxwellpang on 12-9-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FromQQFriendViewController.h"
#import "MyQYaViewController.h"
#import "QQFriendFolder.h"
#import "QQFriendViewCell.h"
#import "QQFriendSectionHeaderView.h"
#import "QQFriend.h"

@interface FromQQFriendViewController ()

@end

#define HEADER_HEIGHT 45

@implementation FromQQFriendViewController

@synthesize mQQFriendFolderArray;
@synthesize mTmpCell;
@synthesize mCellNib;
@synthesize mQQFriendTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        QQFriend *friend1;
        friend1 = [[QQFriend alloc] initWithName:@"尔康" headImageName:@"touxiang1"];
        QQFriend *friend2;
        friend2 = [[QQFriend alloc] initWithName:@"紫薇" headImageName:@"touxiang2"];
        QQFriend *friend3;
        friend3 = [[QQFriend alloc] initWithName:@"小燕子" headImageName:@"touxiang3"];
        QQFriend *friend4;
        friend4 = [[QQFriend alloc] initWithName:@"五阿哥" headImageName:@"touxiang4"];
        QQFriend *friend5;
        friend5 = [[QQFriend alloc] initWithName:@"晴格格" headImageName:@"touxiang5"];
        QQFriend *friend6;
        friend6 = [[QQFriend alloc] initWithName:@"小李子" headImageName:@"touxiang6"];
        
        QQFriendFolder *friendFolder1 = [[QQFriendFolder alloc] init];
        friendFolder1.mIsOpen = NO;
        friendFolder1.mFolderName = @"我的好友";
        [friendFolder1.mQQFriendArray addObject:friend1];
        [friendFolder1.mQQFriendArray addObject:friend2];
        [friendFolder1.mQQFriendArray addObject:friend3];
        [friendFolder1.mQQFriendArray addObject:friend4];
        [friendFolder1.mQQFriendArray addObject:friend6];
        
        QQFriendFolder *friendFolder2 = [[QQFriendFolder alloc] init];
        friendFolder2.mIsOpen = NO;
        friendFolder2.mFolderName = @"屌丝";
        [friendFolder2.mQQFriendArray addObject:friend1];
        [friendFolder2.mQQFriendArray addObject:friend2];
        
        QQFriendFolder *friendFolder3 = [[QQFriendFolder alloc] init];
        friendFolder3.mIsOpen = NO;
        friendFolder3.mFolderName = @"女朋友";
        [friendFolder3.mQQFriendArray addObject:friend5];
        
        mQQFriendFolderArray = [[NSMutableArray alloc] init];
        [mQQFriendFolderArray addObject:friendFolder1];
        [mQQFriendFolderArray addObject:friendFolder2];
        [mQQFriendFolderArray addObject:friendFolder3];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(selectConfirmAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    UIBarButtonItem *selectAll = [[UIBarButtonItem alloc] initWithTitle:@"  全选  " style:UIBarButtonItemStyleBordered target:self action:@selector(selectAllAction:)];
    UIBarButtonItem *selectInvert = [[UIBarButtonItem alloc] initWithTitle:@"  反选  " style:UIBarButtonItemStyleBordered target:self action:@selector(selectInvert:)];
    UIBarButtonItem *selectClear = [[UIBarButtonItem alloc] initWithTitle:@"清除选择" style:UIBarButtonItemStyleBordered target:self action:@selector(selectClearAction:)];
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    [self setToolbarItems:[NSArray arrayWithObjects:flexItem, flexItem, selectAll, flexItem, selectInvert, flexItem, selectClear, flexItem, flexItem, nil]];
    
    self.mCellNib = [UINib nibWithNibName:@"QQFriendViewCell" bundle:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.mTmpCell = nil;
    self.mCellNib = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)selectConfirmAction:(id)sender  
{  
//    [self.navigationController setToolbarHidden:YES animated:NO];
    MyQYaViewController *secondView = [[MyQYaViewController alloc] init];  
    [self.navigationController pushViewController:secondView animated:YES];  
    secondView.title = @"我的Q芽";  
} 

//好友全选
-(void)selectAllAction:(id)sender  
{  
    
}

//好友反选
-(void)selectInvert:(id)sender  
{  
    
}

//清除选择
-(void)selectClearAction:(id)sender  
{  
    
}

#pragma mark Table view data source and delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    
    return [mQQFriendFolderArray count];
}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    
	QQFriendFolder *qqFriendFolder = [mQQFriendFolderArray objectAtIndex:section];
	
    return qqFriendFolder.mIsOpen ? [qqFriendFolder.mQQFriendArray count] : 0;
}


-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    
    static NSString *kCustomCellID = @"QQFriendViewCell";
	
	QQFriendViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
    
	if (cell == nil)
	{
        [self.mCellNib instantiateWithOwner:self options:nil];
        cell = mTmpCell;
        self.mTmpCell = nil;
	}
    
    QQFriendFolder *qqFriendFolder = [mQQFriendFolderArray objectAtIndex:indexPath.section];
    NSString *qqName = ((QQFriend *)[qqFriendFolder.mQQFriendArray objectAtIndex:indexPath.row]).mName;
    
    [cell.mHeadImageView setImage:((QQFriend *)[qqFriendFolder.mQQFriendArray objectAtIndex:indexPath.row]).mHeadImage];
    [cell.mNickNameLabel setText:qqName];
	
	return cell;
}

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {

    /*
     Create the section header views lazily.
     */
	QQFriendFolder *sectionInfo = [self.mQQFriendFolderArray objectAtIndex:section];
    if (!sectionInfo.mHeadView) {
		NSString *playName = sectionInfo.mFolderName;
        sectionInfo.mHeadView = [[QQFriendSectionHeaderView alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, HEADER_HEIGHT) title:playName section:section isOpen:sectionInfo.mIsOpen delegate:self];
    }
    
    sectionInfo.mHeadView.isOpenLabel.text = (!sectionInfo.mIsOpen)?@"》":@"︾";
    sectionInfo.mHeadView.titleLabel.text = sectionInfo.mFolderName;
    return sectionInfo.mHeadView;
}

#pragma mark Section header delegate

-(void)sectionHeaderView:(QQFriendSectionHeaderView *)sectionHeaderView sectionOpened:(NSInteger)sectionOpened {
    
	QQFriendFolder *sectionInfo = [self.mQQFriendFolderArray objectAtIndex:sectionOpened];
	sectionInfo.mIsOpen = !sectionInfo.mIsOpen;
    [mQQFriendTableView reloadData];
}

-(void) viewWillDisappear:(BOOL)animated    
{   
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)    
    {   
        // back button was pressed.  We know this is true because self is no longer   
        // in the navigation stack.     
        NSLog(@"viewWillDisappear:(BOOL)animated ");   
        [self.navigationController setToolbarHidden:YES animated:NO];
        
    }   
    [super viewWillDisappear:animated];   
} 

@end
